package com.richest.api.controller.pay;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.richest.api.controller.form.UnifiedOrderForm;
import com.richest.api.service.IEatOrderService;
import com.richest.api.utils.CommonUtil;
import com.richest.bean.EatOrderBean;
import com.richest.util.CallResults;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 微信支付业务逻辑
 * 
 * @author 赵鸿超 2017年9月24日
 */
@Api(value = "支付接口")
@RestController
@RequestMapping("payController")
@EnableAutoConfiguration
public class payController {
//	private static Logger logger = LoggerFactory.getLogger(payController.class);
	@Autowired
	private IEatOrderService eatOrderSrevice;

	/**
	 * 生成微信支付订单
	 * 
	 * @author 赵鸿超 2017年9月24日
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping(value = "wxUnifiedOrder")
	@ApiOperation(notes = "统一下单接口", value = "统一下单接口", httpMethod = "POST")
	@ApiImplicitParam(name = "form", value = "统一下单参数", required = true, dataType = "UnifiedOrderForm")
	public CallResults wxUnifiedOrder(@RequestBody UnifiedOrderForm form, HttpServletRequest request) {
		Map<String, String> result = new HashMap<>();
		EatOrderBean eatOrderBean = eatOrderSrevice.queryByEatSerialNumber(form.getOut_trade_no(), null);
		if (null == eatOrderBean) {
			return CallResults.error("订单不存在");
		}
		/*
		 * 将价格转换为以分为单位
		 */
		double dTempPrice = 0;
		if (null != eatOrderBean.getPay_money() && !"".equals(eatOrderBean.getPay_money())) {
			BigDecimal bdTempPrice = eatOrderBean.getPay_money();
			BigDecimal bdTempPrice1 = new BigDecimal(100);
			dTempPrice = new BigDecimal(bdTempPrice.multiply(bdTempPrice1).doubleValue()).doubleValue();
		}
		String strTotalFee = dTempPrice + "";
		form.setTotal_fee(strTotalFee.substring(0, strTotalFee.lastIndexOf(".")));
		// 为了测试，价格写成1分
		// form.setTotal_fee("1");

		form.setSpbill_create_ip(CommonUtil.getIpAddr(request)); // 订单生成的机器 IP

		WxPay wxPay = new WxPay();
		try {
			result = wxPay.wxUnifiedOrder(form);
		} catch (Exception e) {
			e.printStackTrace();
			return CallResults.error(e.getMessage());
		}
		return CallResults.ok().put("data", result);
	}

	/**
	 * 生成支付宝支付订单
	 * 
	 * @author 赵鸿超 2017年9月24日
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping(value = "aliUnifiedOrder")
	@ApiOperation(notes = "统一下单接口", value = "统一下单接口", httpMethod = "POST")
	@ApiImplicitParam(name = "form", value = "统一下单参数", required = true, dataType = "UnifiedOrderForm")
	public CallResults aliUnifiedOrder(@RequestBody UnifiedOrderForm form, HttpServletRequest request)
			throws AlipayApiException {

		JSONObject resultObject = new JSONObject();
		EatOrderBean eatOrderBean = eatOrderSrevice.queryByEatSerialNumber(form.getOut_trade_no(), null);
		if (null == eatOrderBean) {
			return CallResults.error("订单不存在");
		}
		/*
		 * 将价格转换为以分为单位
		 */
		BigDecimal bigDTempPrice = new BigDecimal("0");
		if (null != eatOrderBean.getPay_money() && !"".equals(eatOrderBean.getPay_money())) {
			bigDTempPrice = eatOrderBean.getPay_money();
		}
		// 为了测试，价格写成1分
		// form.setTotal_fee("0.01");
		form.setTotal_fee(String.valueOf(bigDTempPrice));

		form.setSpbill_create_ip(CommonUtil.getIpAddr(request)); // 订单生成的机器 IP

		AliPay aliPay = new AliPay();
		try {
			resultObject = aliPay.aliUnifiedOrder(form);
		} catch (Exception e) {
			e.printStackTrace();
			return CallResults.error(e.getMessage());
		}
		return CallResults.ok().put("data", resultObject);
	}

}
